/* server.js */
const exp = require('express');
const path = require('path');
const resolve = file => path.resolve(__dirname, file);
const express = exp();

const template = require('fs').readFileSync('./src/index.template.html', 'utf-8');
const bundle = require('./dist/vue-ssr-server-bundle.json');
const clientManifest = require('./dist/vue-ssr-client-manifest.json');
/**
 * createBundleRenderer 需要 clientManifest 的配合，否则会报错
 * @type {BundleRenderer}
 */
const renderer = require('vue-server-renderer').createBundleRenderer(bundle, {
    template,
    clientManifest,
    basedir: resolve('./dist'),
    runInNewContext: false
});

// 设置静态文件目录
express.use('/', exp.static(__dirname + '/dist'));




// 响应路由请求
express.get('*', (req, res) => {
    const context = { url: req.url }
    renderer.renderToString(context, (err, html) => {
        res.send(html);
    })
});


// 服务器监听地址
express.listen(8080, () => {
    console.log('服务器已启动！')
})